//
//  ECUIView_ListHeader.m
//  Edu_iOS2018
//
//  Created by yanyan on 2018/1/6.
//  Copyright © 2018年 yanyan. All rights reserved.
//

#import "ECUIView_ListHeader.h"
#import "ECUIButton_Category.h"
#import "ECUIButton_Sort.h"
@interface ECUIView_ListHeader()
@property(nonatomic,strong)UIButton *integratedBtn;// 综合
@property(nonatomic,strong)UIButton *popularBtn;// 人气
@property(nonatomic,strong)ECUIButton_Sort *priceBtn;// 价格

//@property(nonatomic,strong)ECUIButton_Category *allCategoryBtn;// 分类

@end
@implementation ECUIView_ListHeader
@synthesize allCategoryBtn = _allCategoryBtn;
- (void)addSubviews
{
    [super addSubviews];
    CGFloat frontWidth = 0.2 * kScreenWidth;
    CGFloat backWidth = 0.4 * kScreenWidth - 1;
    CGFloat btnHeight = self.frame.size.height - 1;
    UIFont *font = [UIFont systemFontOfSize:13*kPercentage];
    UIColor *normalColor = [UIColor colorWithHexString:@"#4d4d4d"];
    WeakSelf(weakSelf);
    
    _integratedBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _integratedBtn.titleLabel.font = font;
    [_integratedBtn setTitle:@"综合" forState:UIControlStateNormal];
    [_integratedBtn setTitleColor:normalColor forState:UIControlStateNormal];
    [_integratedBtn setTitleColor:ECTHEMESELECTEDCOLOR forState:UIControlStateSelected];
    [_integratedBtn addTarget:self action:@selector(action_integrateBtn:) forControlEvents:UIControlEventTouchUpInside];
    _integratedBtn.frame = CGRectMake(0, 0, frontWidth, btnHeight);
    _integratedBtn.selected = YES;
    [self addSubview:_integratedBtn];
    // 人气
    _popularBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _popularBtn.frame = CGRectMake(frontWidth, 0, frontWidth, btnHeight);
    _popularBtn.titleLabel.font = font;

    [_popularBtn setTitle:@"人气" forState: UIControlStateNormal];
    [_popularBtn setTitleColor:normalColor forState:UIControlStateNormal];
    [_popularBtn setTitleColor:ECTHEMESELECTEDCOLOR forState:UIControlStateSelected];
    [_popularBtn setImage:[UIImage imageNamed:@"category_sort_normal_down"] forState:UIControlStateNormal];
    [_popularBtn setImage:[UIImage imageNamed:@"category_sort_select_down"] forState:UIControlStateSelected];
    CGSize size2 = [[_popularBtn titleForState:UIControlStateNormal] sizeWithAttributes:@{NSFontAttributeName: _popularBtn.titleLabel.font}];
    _popularBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -13*kPercentage, 0, 13 * kPercentage);
    _popularBtn.imageEdgeInsets = UIEdgeInsetsMake((40 - 7 *kPercentage)/2, size2.width-13*kPercentage , (40 - 7 * kPercentage)/2, -size2.width-5);
    [_popularBtn addTarget:self action:@selector(action_popularBtn:) forControlEvents:UIControlEventTouchUpInside];

    [self addSubview:_popularBtn];
    //  价格
    _priceBtn = [[ECUIButton_Sort alloc]initWithFrame: CGRectMake(frontWidth*2, 0, frontWidth, btnHeight)];
    [_priceBtn setTitle:@"价格" forState:UIControlStateNormal];
    _priceBtn.titleLabel.font = font;
    [_priceBtn setImage:[UIImage imageNamed:@"category_sort_price_normal"] forState:UIControlStateNormal];
    [_priceBtn setImage:[UIImage imageNamed:@"category_sort_select_down"] forState:UIControlStateSelected];
    CGSize size3 = [[_priceBtn titleForState:UIControlStateNormal] sizeWithAttributes:@{NSFontAttributeName: _priceBtn.titleLabel.font}];
    _priceBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -9*kPercentage, 0, 9 * kPercentage);
//    _priceBtn.imageEdgeInsets = UIEdgeInsetsMake((40 - 5 * kPercentage)/2, size3.width-9*kPercentage , (40 - 5 * kPercentage)/2, -size3.width-9*kPercentage);
    _priceBtn.imageEdgeInsets = UIEdgeInsetsMake(10, size3.width-9*kPercentage , 10, -size3.width-9*kPercentage);

    [_priceBtn setTitleColor:normalColor forState:UIControlStateNormal];
    [_priceBtn setTitleColor:ECTHEMESELECTEDCOLOR forState:UIControlStateSelected];
    [_priceBtn addTarget:self action:@selector(action_priceBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_priceBtn];
    
    
    UIView *lineView = [[UIView alloc]init];
    lineView.backgroundColor = [UIColor colorWithRGBRed:214 green:214 blue:214];
    lineView.frame = CGRectMake(frontWidth *3, 10, 1, 20);
    [self addSubview:lineView];

    [self addSubview:self.allCategoryBtn];
    _allCategoryBtn.titleLabel.font = font;
    CGSize size = [[_allCategoryBtn titleForState:UIControlStateNormal] sizeWithAttributes:@{NSFontAttributeName: _allCategoryBtn.titleLabel.font}];
    _allCategoryBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
    _allCategoryBtn.frame = CGRectMake(CGRectGetMaxX(lineView.frame), 0, backWidth, btnHeight);
    _allCategoryBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -13*kPercentage, 0, 13 * kPercentage);
    _allCategoryBtn.imageEdgeInsets = UIEdgeInsetsMake((40 - 7*kPercentage)/2, size.width+2 , (40 - 7 *kPercentage)/2, -size.width-2);
    _allCategoryBtn.block_selected = ^(id sender){
        [weakSelf action_allCategoryBtn:sender];
    };
    UIView *bottomLine = [[UIView alloc]init];
    bottomLine.backgroundColor = [UIColor colorWithRGBRed:214 green:214 blue:214];
    bottomLine.frame = CGRectMake(0, CGRectGetMaxY(_allCategoryBtn.frame)-0.5, kScreenWidth, 0.5);
    [self addSubview:bottomLine];
    
}
- (ECUIButton_Category *)allCategoryBtn{
    if (!_allCategoryBtn) {
        WeakSelf(weakSelf);
        _allCategoryBtn = [[ECUIButton_Category alloc]initWithNormalImage:[UIImage imageNamed:@"category_sort_normal_down"] selectedImage:[UIImage imageNamed:@"category_sort_select_down"]];
        [_allCategoryBtn setTitle:@"全部" forState:UIControlStateNormal];
        [_allCategoryBtn setTitle:@"全部" forState:UIControlStateSelected];
        _allCategoryBtn.selectedState = KHButtonStateSortDown;
    }
    return _allCategoryBtn;
}

- (void)action_integrateBtn:(UIButton *)sender
{
    if (sender.isSelected) {
        return;
    }
    sender.selected = !sender.isSelected;
    _popularBtn.selected = NO;
    _priceBtn.selectedState = KHButtonStateNormal;
    if (self.block_selectedSort) {
        self.block_selectedSort(@{});
    }
}
-(void)action_popularBtn:(UIButton *)sender
{
    if (sender.isSelected) {
        return;
    }
    sender.selected = !sender.isSelected;
    _integratedBtn.selected = NO;
    _priceBtn.selectedState = KHButtonStateNormal;
    if (self.block_selectedSort) {
        self.block_selectedSort(@{@"sort":@"popular"});
    }
}
- (void)action_priceBtn:(UIButton *)sender{
    _popularBtn.selected = NO;
    _integratedBtn.selected = NO;
    NSDictionary *dict = nil;
    if ([sender isKindOfClass:[ECUIButton_Sort class]]) {
        if (((ECUIButton_Sort *)sender).selectedState == KHButtonStateNormal) {
            ((ECUIButton_Sort *)sender).selectedState = KHButtonStateSortDown;
            dict = @{@"sort":@"priceDesc"};
        } else if (((ECUIButton_Sort *)sender).selectedState == KHButtonStateSortDown) {
            ((ECUIButton_Sort *)sender).selectedState = KHButtonStateSortUp;
            dict = @{@"sort":@"priceAsc"};

        } else {
            ((ECUIButton_Sort *)sender).selectedState = KHButtonStateSortDown;
            dict = @{@"sort":@"priceDesc"};
        }
    }
    if (self.block_selectedSort) {
        self.block_selectedSort(dict);
    }
//        if ([sender isKindOfClass:[ECUIButton_Sort class]]) {
//            if (((ECUIButton_Sort *)sender).selectedState == KHButtonStateNormal) {
//                ((ECUIButton_Sort *)sender).selectedState = KHButtonStateSortDown;
//            } else if (((ECUIButton_Sort *)sender).selectedState == KHButtonStateSortDown) {
//                ((ECUIButton_Sort *)sender).selectedState = KHButtonStateSortUp;
//
//            } else {
//                ((ECUIButton_Sort *)sender).selectedState = KHButtonStateNormal;
//
//            }
//        }
}
-(void)action_allCategoryBtn:(id)sender{
    KHButtonState selectedState = ((ECUIButton_Category *)sender).selectedState;
    
    if (self.block_categoryBtn) {
        self.block_categoryBtn(sender);
    }
   
}
-(void)setSelectCategory:(PrimaryCategoryModel *)mo{
    if (mo) {
        _allCategoryBtn.selected = YES;
        [_allCategoryBtn setTitle:mo.title forState:UIControlStateNormal];
        [_allCategoryBtn setTitle:mo.title forState:UIControlStateSelected];

        CGSize size = [[_allCategoryBtn titleForState:UIControlStateNormal] sizeWithAttributes:@{NSFontAttributeName: _allCategoryBtn.titleLabel.font}];
        if (size.width > _allCategoryBtn.width - 25) {
            size.width = _allCategoryBtn.width - 25;
        };
        _allCategoryBtn.titleEdgeInsets = UIEdgeInsetsMake(0, -13*kPercentage, 0, 13 * kPercentage);
        _allCategoryBtn.imageEdgeInsets = UIEdgeInsetsMake((40 - 7*kPercentage)/2, size.width+2 , (40 - 7 *kPercentage)/2, -size.width-2);
    }
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
